﻿/// <reference path="jquery-1.2.6.min.js" />

$(document).ready(function() {
    var objDiv = document.getElementById('divChatHistory');
    objDiv.scrollTop = objDiv.scrollHeight;

    var recipientName = document.getElementById('hfRecipient').value; //Contains the GUID of the Chat.
    var senderName = document.getElementById('hfSender').value;
    //sends a request to get messages from the database.
    chatProg.getGroupMessages(senderName, recipientName, '0', OnWSRequestCompleteGroup);

    $.timer(2000, function(timer) {
        //gets messages every 2 seconds.
        getGroupMessages(senderName, recipientName);
    });
    $.timer(10000, function(timer) {
        //updates the group list every 10 seconds.
        updateList();
    });
});

///<summary>
/// sends the message to be inserted into the databse.
///</summary>
///<param name="message" optional="false">this is the actual message.</param>
function SetGroupMessages(message) {
    var senderName = document.getElementById('hfRecipient').value;
    var recipientName = document.getElementById('hfSender').value;
    chatProg.setGroupMessage(message, recipientName, senderName);
}

///<summary>
/// gets the messages from the database.
///</summary>
///<param name="sender" optional="false">the sender user name</param>
///<param name="recipient" optional="false">the recipient user name</param>
function getGroupMessages(sender, recipient) {
    //alert(recipient);
    var lastUid = document.getElementById('tbChatID').value;
    chatProg.getGroupMessages(sender, recipient, lastUid, OnWSRequestCompleteGroup);
};

///<summary>
/// returns the messages from the database
///</summary>
///<param name="results" optional="false">the messages in a string[] value</param>
function OnWSRequestCompleteGroup(results) {
    var objDiv = document.getElementById('divChatHistory');
    if (results[1] != null)
        document.getElementById('tbChatID').value = results[1];
    if (results[0] != null) {
        objDiv.innerHTML += results[0];
    }
    objDiv.scrollTop = objDiv.scrollHeight;
};

///<summary>
/// sends a request to update the group list.
///</summary>
function updateList() {
    chatProg.getUsers(document.getElementById('hfRecipient').value, setList);
}

///<summary>
/// sets the lists content
///</summary>
///<param name="results" optional="false">sets the results of the list.</param>
function setList(results) {
    clearlistbox(document.getElementById("lbUsersInChat"));
    // Create an Option object        
    // Add an Option object to Drop Down/List Box
    for (var i = 0; i < results.length; i++) {
        var optOther = document.createElement("option");
        document.getElementById("lbUsersInChat").options.add(optOther);
        // Assign text and value to Option object
        optOther.text = results[i];
        optOther.value = results[i];
        delete optOther;
    }
}

///<summary>
/// loads when a name on the chat box is double clicked.
///</summary>
function lstDblClicked() {
    //find the clicked on name and it opens up a chat box jsut for that name.
    for (var i = 0; i < document.getElementById("lbUsersInChat").options.length; i++) {
        if (document.getElementById("lbUsersInChat").options[i].selected) {
            commonPopup('chat.aspx?r=' + document.getElementById("lbUsersInChat").options[i].text, 400, 350, 4, 'Chat' + document.getElementById("lbUsersInChat").options[i].text);
        }
    }
}

